package top.lyc829.leetcode.T347;

import java.util.*;

public class Solution {
    public int[] topKFrequent(int[] nums, int k) {

        Map<Integer, Integer> map = new HashMap<>();

        for (int num : nums) {
            map.merge(num, 1, Integer::sum);
        }

        List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());

        list.sort((o1, o2) -> o2.getValue() - o1.getValue());

        int[] res = new int[k];
        for (int i = 0; i < k; i++) {
            res[i] = list.get(i).getKey();
        }
        return res;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        int[] res = s.topKFrequent(new int[] {1,1,1,2,2,3}, 2);
        System.out.println(Arrays.toString(res));
    }

}
